package com.liu.controller;

import com.liu.common.ParentTemplate;
import com.liu.common.Template;
import com.liu.utils.MyStringUtils;

/**
 * @author Mr.Liu
 * @version 1.0
 * @date 2024/8/31 16:26
 */
public class ControllerTemplate  extends ParentTemplate  implements Template {
    @Override
    public String toTemplate() {

        String  CONTROLLER_VAR = MyStringUtils.toLowerFirstChar(this.getClassName());  //转小写开头
        String GET_ID_FUNC  = "get"+ MyStringUtils.capitalizeFirstChar(this.getPrimaryKey())+"()";
        StringBuffer sb = new StringBuffer();
        sb.append("package "+this.getPackageName()+".controller;\n");
        sb.append("import com.ais.framework.common.CommonParent;\n" +
                "import com.ais.framework.entity.RestEntity;\n" +
                "import com.ais.framework.model.SysManageModel;\n" +
                "import com.ais.framework.service."+this.getClassName()+"Service;\n" +
                "import jakarta.annotation.Resource;\n" +
                "import org.apache.commons.lang3.StringUtils;\n" +
                "import org.springframework.web.bind.annotation.RequestBody;\n" +
                "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                "import org.springframework.web.bind.annotation.RestController;\n" +
                "\n" +
                "import java.util.HashMap;\n" +
                "import java.util.List;\n" +
                "import java.util.Map;\n");
        sb.append("@RestController\n" +
                "@RequestMapping(\"/back/"+CONTROLLER_VAR+"\")\n");
        sb.append("public class "+this.getClassName()+"Controller extends CommonParent {\n");
        sb.append("    @Resource\n");
        sb.append("    private "+this.getClassName()+"Service "+CONTROLLER_VAR+"Service;\n\n");


        //search start
        sb.append("    @RequestMapping(\"/search\")\n");
        sb.append("    public Object search(@RequestBody "+this.getClassName()+"Model model) {\n");
        sb.append("        Map<String, Object> map = new HashMap<>(2);\n");
        sb.append("        List<"+this.getClassName()+"Model> list = "+CONTROLLER_VAR+"Service.search(model);\n");
        sb.append("        map.put(\"list\", list);\n");
        sb.append("        map.put(\"total\", "+CONTROLLER_VAR+"Service.searchCount(model));\n");
        sb.append("        return RestEntity.success(map);\n");
        sb.append("    }\n");
        //search end

        //searchById start
        sb.append("    @RequestMapping(\"/searchById\")\n");
        sb.append("    public Object searchById(@RequestBody Integer "+this.getPrimaryKey()+") {\n");
        sb.append("        "+this.getClassName()+"Model model = "+CONTROLLER_VAR+"Service.searchById("+this.getPrimaryKey()+");\n");
        sb.append("        return RestEntity.success(model);\n");
        sb.append("    }\n");
        //searchById end

        //insert start
        sb.append("    @RequestMapping(\"/insert\")\n");
        sb.append("    public Object insert(@RequestBody "+this.getClassName()+"Model model) {\n");
        sb.append("        model.setCreatedBy(getUserCode());\n");
        sb.append("        Object object = "+CONTROLLER_VAR+"Service.insert(model);\n");
        sb.append("        return RestEntity.decide(object);\n");
        sb.append("    }\n");
        //insert end


        //update start
        sb.append("    @RequestMapping(\"/update\")\n");
        sb.append("    public Object update(@RequestBody "+this.getClassName()+"Model model) {\n");
        sb.append("        model.setLastUpdatedBy(getUserCode());\n");
        sb.append("        Object object = "+CONTROLLER_VAR+"Service.update(model);\n");
        sb.append("        return RestEntity.decide(object);\n");
        sb.append("    }\n");
        //update end

        //delete start
        sb.append("    @RequestMapping(\"/delete\")\n");
        sb.append("    public Object delete(@RequestBody "+this.getClassName()+"Model model) {\n");
        sb.append("        if(model."+GET_ID_FUNC+" == null) {\n" +
                "            return RestEntity.failed(\"DATA_ERR\");\n" +
                "        }\n");
        sb.append("        model.setLastUpdatedBy(getUserCode());\n");
        sb.append("        Object object = "+CONTROLLER_VAR+"Service.delete(model);\n");
        sb.append("        return RestEntity.decide(object);\n");
        sb.append("    }\n");
        //delete end


        //check start TODO ...暂时不做

        //check end


        sb.append("}");

        return sb.toString();
    }
}
